package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.FormalStudent;
import cn.wolfcode.edu.domain.LeavingStudent;
import cn.wolfcode.edu.mapper.FormalStudentMapper;
import cn.wolfcode.edu.mapper.LeavingStudentMapper;
import cn.wolfcode.edu.mapper.LeavingStudentMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.ILeavingStudentService;
import cn.wolfcode.edu.service.ILeavingStudentService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by NEUZJ on 2018/7/8.
 */
@Service
public class LeavingStudentServiceImpl implements ILeavingStudentService {
    @Autowired
    private LeavingStudentMapper leavingStudentMapper;
    @Autowired
    private FormalStudentMapper formalStudentMapper;


    @Override
    public void save(LeavingStudent leavingStudent) {

        //获得退学人员的id,即是正式学员时候的id
        Long id=leavingStudent.getId();
        formalStudentMapper.updateStatus(id,FormalStudent.LOSE_AUDIT);
        FormalStudent formalStudent=formalStudentMapper.selectByPrimaryKey(id);
        //获取其姓名
        leavingStudent.setName(formalStudent.getStudentName());
        //获取学员电话
        System.out.print(formalStudent.getTel());
        leavingStudent.setTel(formalStudent.getTel());
        System.out.print(leavingStudent.getTel());
        leavingStudent.setClassGrade(formalStudent.getClz().getName());
        leavingStudent.setLeavingTime(new Date());
        leavingStudent.setMarketer(formalStudent.getSaleman().getRealName());
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        leavingStudent.setOperator(employee.getUsername());
        leavingStudentMapper.insert(leavingStudent);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int  total=leavingStudentMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List list=leavingStudentMapper.queryForList(qo);
        return new PageResult(total,list);
    }

    @Override
    public void update(LeavingStudent leavingStudent) {
        System.out.print(leavingStudent);
        leavingStudentMapper.updateByPrimaryKey(leavingStudent);
    }

    @Override
    public void delete(Long id) {
    }

    @Override
    public void aduit(String tel,Long id) {
        //先改变流失学员的审核状态
        System.out.println(tel);
        System.out.println(id);
        leavingStudentMapper.changeAduitStatus(id,LeavingStudent.ADUIT_STATUS);
        //通过唯一性的电话号码,去正式学员那里改变审核状态
        formalStudentMapper.changeStatus(tel,FormalStudent.LOSE_STATUS);
    }

}
